<template>
  <el-dialog
    :title="!dataForm.consultOrderId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="名称"></el-input>
    </el-form-item>
    <el-form-item label="年龄" prop="age">
      <el-input v-model="dataForm.age" placeholder="年龄"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="sex">
       <el-select v-model="dataForm.sex" placeholder="请选择">
            <el-option  v-for="(item) in sexs" :key="item.value" :label="item.label" :value="item.value"></el-option>
         </el-select>
    </el-form-item>
    <el-form-item label="手机号" prop="phone">
      <el-input v-model="dataForm.phone" placeholder="手机号"></el-input>
    </el-form-item>
    <el-form-item label="咨询方向" prop="consultDirection">
      <el-input v-model="dataForm.consultDirection" placeholder="咨询方向"></el-input>
    </el-form-item>
    <el-form-item label="咨询原因" prop="consultReason">
      <el-input v-model="dataForm.consultReason" placeholder="咨询原因"></el-input>
    </el-form-item>
    <el-form-item label="身份证号码" prop="idCardNumber" label-width="95px">
      <el-input v-model="dataForm.idCardNumber" placeholder="身份证号码"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          consultOrderId: 0,
          name: '',
          age: '',
          sex: '',
          phone: '',
          idCardNumber: '',
          consultDirection: '',
          consultReason: '',
        },
        sexs: [{
          value: '0',
          label: '男'
        }, {
          value: '1',
          label: '女'
        }],
        dataRule: {
          name: [
            { required: true, message: '名称不能为空', trigger: 'blur' }
          ],
          age: [
            { required: true, message: '年龄不能为空', trigger: 'blur' }
          ],
          sex: [
            { required: true, message: '性别不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '手机号不能为空', trigger: 'blur' }
          ],
          idCardNumber: [
            { required: true, message: '身份证号码不能为空', trigger: 'blur' }
          ],
          consultDirection: [
            { required: true, message: '咨询方向不能为空', trigger: 'blur' }
          ],
          consultReason: [
            { required: true, message: '咨询原因不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.consultOrderId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.consultOrderId) {
            this.$http({
              url: this.$http.adornUrl(`/heartSelf/shConsultOrder/consultOrderInfo/${this.dataForm.consultOrderId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.shConsultOrder.name
                this.dataForm.age = data.shConsultOrder.age
                this.dataForm.sex = data.shConsultOrder.sex
                this.dataForm.phone = data.shConsultOrder.phone
                this.dataForm.idCardNumber = data.shConsultOrder.idCardNumber
                this.dataForm.consultDirection = data.shConsultOrder.consultDirection
                this.dataForm.consultReason = data.shConsultOrder.consultReason
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/heartSelf/shConsultOrder/${!this.dataForm.consultOrderId ? 'saveConsultOrder' : 'updateConsultOrder'}`),
              method: 'post',
              data: this.$http.adornData({
                'consultOrderId': this.dataForm.consultOrderId || undefined,
                'name': this.dataForm.name,
                'age': this.dataForm.age,
                'sex': this.dataForm.sex,
                'phone': this.dataForm.phone,
                'idCardNumber': this.dataForm.idCardNumber,
                'consultDirection': this.dataForm.consultDirection,
                'consultReason': this.dataForm.consultReason,
                'userId': this.dataForm.userId,
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
